//
//  EeTopStyle-default.swift
//  ETNavBarTransparentDemo
//
//  Created by zhu xietong on 2017/9/29.
//  Copyright © 2017年 zhuxieong. All rights reserved.

import Foundation
import UIKit
private func defaultStyle()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = UIColor.darkText
    style.statusBarStyle = .default
    style.navBarBgAlpha = 1
    style.navBarBgStyle = .color(UIColor.white)
    style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.darkText]
    style.navBarShadow = .color(UIColor(white: 0.3, alpha: 0.5))
    return style
}

private func whiteStyle()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = EeTopStyle.default.navTintColor
    style.statusBarStyle = .default
    style.navBarBgAlpha = 1
    style.navBarBgStyle = .color(UIColor.white)
    style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.darkText]
    style.navBarShadow = .color(UIColor(white: 0.3, alpha: 0.5))
    return style
}

private func redStyle()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = .white
    style.statusBarStyle = .lightContent
    style.navBarBgStyle = .color(.red)
    style.navBarBgAlpha = 1
    return style
}


private func blueStyle()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = .white
    style.statusBarStyle = .lightContent
    style.navBarBgStyle = .color(.blue)
    style.navBarBgAlpha = 1
    return style
}

private func transparentLight()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = .white
    style.statusBarStyle = .lightContent
    style.navBarBgAlpha = 0
    style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor(white: 1, alpha: 1)]
    return style
}

private func transparentDark()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = .darkGray
    style.statusBarStyle = .default
    style.navBarBgAlpha = 0
    return style
}


private func blurLight()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = .white
    style.statusBarStyle = .lightContent
    style.navBarBgStyle = .blur(.light)
    style.navBarBgAlpha = 1
    return style
}

private func blurDark()->EeTopStyle
{
    var style = EeTopStyle()
    style.navTintColor = .white
    style.statusBarStyle = .lightContent
    style.navBarBgStyle = .blur(.dark)
    style.navBarBgAlpha = 1
    style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]

    return style
}




extension EeTopStyle{
    public static func light(with color:UIColor)->EeTopStyle{
        var style = EeTopStyle()
        style.navTintColor = .white
        style.statusBarStyle = .lightContent
        style.navBarBgStyle = .color(color)
        style.navBarBgAlpha = 1
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        return style
    }
    
    public static func dark(with color:UIColor)->EeTopStyle{
        var style = EeTopStyle()
        style.navTintColor = .darkText
        style.statusBarStyle = .default
        style.navBarBgStyle = .color(color)
        style.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.darkText]
        
        style.navBarBgAlpha = 1
        return style
    }
    
    public static var `default`:EeTopStyle = defaultStyle()
    
    public static var white:EeTopStyle = whiteStyle()
    
    public static var blur_dark:EeTopStyle = blurDark()
    public static var blur_light:EeTopStyle = blurLight()
    
    
    
    public static var transparent_dark:EeTopStyle = transparentDark()
    public static var transparent_light:EeTopStyle = transparentLight()
    
    public static var orange:EeTopStyle = light(with: UIColor(hexs:"#f39c12"))
    public static var yellow:EeTopStyle = light(with: UIColor(hexs:"#f1c40f"))
    public static var green:EeTopStyle = light(with: UIColor(hexs:"#1abc9c"))
    public static var green_grass:EeTopStyle = light(with: UIColor(hexs:"#2ecc71"))
    public static var blue:EeTopStyle = light(with: UIColor(hexs:"#2980b9"))
    public static var purple:EeTopStyle = light(with: UIColor(hexs:"#9b59b6"))
    public static var purple_deep:EeTopStyle = light(with: UIColor(hexs:"#8e44ad"))
    public static var dark:EeTopStyle = light(with: UIColor(hexs:"#9b59b6"))
    public static var dark_deep:EeTopStyle = light(with: UIColor(hexs:"#2c3e50"))
    
    public static var cloud:EeTopStyle = dark(with: UIColor(hexs:"#ecf0f1"))
    public static var sliver:EeTopStyle = dark(with: UIColor(hexs:"#bdc3c7"))
    
    public static var red:EeTopStyle = light(with: UIColor(hexs:"#e74c3c"))
    public static var red_deep:EeTopStyle = light(with: UIColor(hexs:"#c0392b"))
    
    public static var carrot:EeTopStyle = light(with: UIColor(hexs:"#e67e22"))
    public static var pumpkin:EeTopStyle = light(with: UIColor(hexs:"#d35400"))
    
}

extension UIColor{
    
    public func alpha(_ alpha:CGFloat) -> UIColor {
        var fromRed: CGFloat = 0
        var fromGreen: CGFloat = 0
        var fromBlue: CGFloat = 0
        var fromAlpha: CGFloat = 0
        self.getRed(&fromRed, green: &fromGreen, blue: &fromBlue, alpha: &fromAlpha)
     
        return UIColor(red: fromRed, green: fromGreen, blue: fromBlue, alpha: alpha)
    }
    public func mix(color:UIColor,percent:CGFloat) -> UIColor {
        return .averageColor(fromColor: self, toColor: color, percent: percent)
    }
    public static func averageColor(fromColor: UIColor, toColor: UIColor, percent: CGFloat) -> UIColor {
        var fromRed: CGFloat = 0
        var fromGreen: CGFloat = 0
        var fromBlue: CGFloat = 0
        var fromAlpha: CGFloat = 0
        fromColor.getRed(&fromRed, green: &fromGreen, blue: &fromBlue, alpha: &fromAlpha)
        
        var toRed: CGFloat = 0
        var toGreen: CGFloat = 0
        var toBlue: CGFloat = 0
        var toAlpha: CGFloat = 0
        toColor.getRed(&toRed, green: &toGreen, blue: &toBlue, alpha: &toAlpha)
        
        let nowRed = fromRed + (toRed - fromRed) * percent
        let nowGreen = fromGreen + (toGreen - fromGreen) * percent
        let nowBlue = fromBlue + (toBlue - fromBlue) * percent
        let nowAlpha = fromAlpha + (toAlpha - fromAlpha) * percent
        return UIColor(red: nowRed, green: nowGreen, blue: nowBlue, alpha: nowAlpha)
    }
}


private extension UIColor {
    
    
    
    convenience init(hexs hex: String) {
        var red:   CGFloat = 0.0
        var green: CGFloat = 0.0
        var blue:  CGFloat = 0.0
        var alpha: CGFloat = 1.0
        var hex:   String = hex
        
        if hex.hasPrefix("#") {
            let index = hex.index(hex.startIndex, offsetBy: 1)
            hex = String(hex[index..<hex.endIndex])
            
        }
        
        let scanner = Scanner(string: hex)
        var hexValue: CUnsignedLongLong = 0
        if scanner.scanHexInt64(&hexValue) {
            switch (hex.count) {
            case 3:
                red   = CGFloat((hexValue & 0xF00) >> 8)       / 15.0
                green = CGFloat((hexValue & 0x0F0) >> 4)       / 15.0
                blue  = CGFloat(hexValue & 0x00F)              / 15.0
            case 4:
                red   = CGFloat((hexValue & 0xF000) >> 12)     / 15.0
                green = CGFloat((hexValue & 0x0F00) >> 8)      / 15.0
                blue  = CGFloat((hexValue & 0x00F0) >> 4)      / 15.0
                alpha = CGFloat(hexValue & 0x000F)             / 15.0
            case 6:
                red   = CGFloat((hexValue & 0xFF0000) >> 16)   / 255.0
                green = CGFloat((hexValue & 0x00FF00) >> 8)    / 255.0
                blue  = CGFloat(hexValue & 0x0000FF)           / 255.0
            case 8:
                red   = CGFloat((hexValue & 0xFF000000) >> 24) / 255.0
                green = CGFloat((hexValue & 0x00FF0000) >> 16) / 255.0
                blue  = CGFloat((hexValue & 0x0000FF00) >> 8)  / 255.0
                alpha = CGFloat(hexValue & 0x000000FF)         / 255.0
            default:
                break
            }
        } else {
        }
        self.init(red:red, green:green, blue:blue, alpha:alpha)
    }
}





